public class Test02 {
    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }

    //方法二：
    //利用递归
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        //处理边界条件
        if (list1 == null){
            return list2;
        }
        if (list2 == null){
            return list1;
        }

        //假设 list1 的头结点为最后返回的链表
        if (list1.val < list2.val){
            list1.next = mergeTwoLists(list1.next,list2);
            return list1;
        }else {
            //假设 list2 的头结点为最后返回的链表
            list2.next = mergeTwoLists(list1,list2.next);
            return list2;
        }
    }

    /*public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        //创建一个虚拟节点
        ListNode newNode = new ListNode(0);
        //定义两个指针，同时对两个有序链表进行遍历
        //临时节点
        ListNode tmp = newNode;
        while (list1 != null && list2 != null) {
            if (list1.val < list2.val) {
                tmp.next = list1;
                list1 = list1.next;
                tmp = tmp.next;
            } else {
                tmp.next = list2;
                list2 = list2.next;
                tmp = tmp.next;
            }

        }
        if (list1 == null) {
            //将链表2 添加到新链表
            tmp.next = list2;
        }
        if (list2 == null) {
            tmp.next = list1;
        }
        return newNode.next;
    }*/

    public static void main(String[] args) {

    }
}
